package com.shop.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.entity.SupplierSupplier;
import com.shop.mapper.SupplierSupplierMapper;
import com.shop.model.PageParam;
import com.shop.model.vo.Rsp;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 供应商  yyt(SupplierSupplier)表服务接口
 *
 * @author makejava
 * @since 2020-11-21 10:12:54
 */
@Service
@AllArgsConstructor
public class SupplierSupplierService {

    @Resource
    private final SupplierSupplierMapper supplierSupplierMapper;

    /**
     * 获取供应商列表
     *
     * @return
     */
    public PageInfo<SupplierSupplier> searchSupplierPage(PageParam<SupplierSupplier> pageParam) {
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            return new PageInfo<SupplierSupplier>(supplierSupplierMapper.superSearch(pageParam.getSuperSearchKeyWord()));
        }
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        //排序
        for (int i = 0; i < pageParam.getOrderParams().length; i++) {
            PageHelper.orderBy(pageParam.getOrderParams()[i]);
        }
        return new PageInfo<SupplierSupplier>(supplierSupplierMapper.queryAllByModel(pageParam.getModel()));
    }

    /**
     * 增
     *
     * @param supplierSupplier
     * @return
     */
    public Rsp insertOne(SupplierSupplier supplierSupplier) {
        Integer insert = supplierSupplierMapper.insertOne(supplierSupplier);
        if (insert == 1) {
            return Rsp.success(supplierSupplier).msg("新增成功");
        } else {
            return Rsp.fail().msg("服务异常");
        }
    }

    /**
     * 改
     *
     * @param supplierSupplier
     * @return
     */
    public Rsp updateOne(SupplierSupplier supplierSupplier) {
        Integer updateOne = supplierSupplierMapper.updateOne(supplierSupplier);
        if (updateOne == 1) {
            return Rsp.success(supplierSupplier).msg("修改成功");
        } else {
            return Rsp.fail().msg("服务异常");
        }
    }

    /**
     * 删
     *
     * @param supplierSupplier
     * @return
     */
    public Rsp deleteOne(SupplierSupplier supplierSupplier) {
        Integer id = supplierSupplier.getId();
        Integer deleteOne = supplierSupplierMapper.deleteOne(supplierSupplier);
        if (deleteOne == 1) {
            return Rsp.success(supplierSupplier).msg("删除成功");
        } else {
            return Rsp.fail().msg("服务异常");
        }
    }

    /**
     * 获取供应商列表
     *
     * @return
     */
    public Rsp getSupplierList() {
        List<SupplierSupplier> supplierList = supplierSupplierMapper.getSupplierList();
        return Rsp.success(supplierList);
    }
}
